package com.ethink.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.db.PageUtils;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.system.api.constant.SystemRespCode;
import com.ethink.system.api.info.SystemApiAuthorityInfo;
import com.ethink.system.api.request.SystemApiAuthorityAddRequest;
import com.ethink.system.api.request.SystemApiAuthorityQueryRequest;
import com.ethink.system.api.request.SystemApiAuthorityUpdateRequest;
import com.ethink.system.common.entity.SystemApiAuthority;
import com.ethink.system.common.entity.SystemRoleApiAuthority;
import com.ethink.system.common.mapper.SystemRoleApiAuthorityMapper;
import com.ethink.system.converter.SystemApiAuthorityConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ethink.system.common.mapper.SystemApiAuthorityMapper;
import com.ethink.system.service.SystemApiAuthorityService;

/**
 * 系统接口权限表 Service接口实现
 *
 * @author default
 * @date 2022-09-26
 */
@Slf4j
@Service
public class SystemApiAuthorityServiceImpl implements SystemApiAuthorityService {

    @Autowired
    private SystemApiAuthorityMapper systemApiAuthorityMapper;

    @Autowired
    private SystemApiAuthorityConverter systemApiAuthorityConverter;

    @Autowired
    private SystemRoleApiAuthorityMapper systemRoleApiAuthorityMapper;

    @Override
    public Page<SystemApiAuthorityInfo> getSystemApiAuthorityList(SystemApiAuthorityQueryRequest request) {
        return systemApiAuthorityMapper.querySystemApiAuthority(PageUtils.buildPage(request),request);
    }

    @Override
    public SystemApiAuthorityInfo getSystemApiAuthorityById(Long systemApiAuthorityId) {
        SystemApiAuthority systemApiAuthority = systemApiAuthorityMapper.selectById(systemApiAuthorityId);
        return systemApiAuthorityConverter.convertFrom(systemApiAuthority);
    }

    @Override
    public void addSystemApiAuthority(SystemApiAuthorityAddRequest request) {
        LambdaQueryWrapper<SystemApiAuthority> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SystemApiAuthority::getApiCode,request.getApiCode());
        wrapper.eq(SystemApiAuthority::getIsDeleted,false);
        SystemApiAuthority systemApiAuthority = systemApiAuthorityMapper.selectOne(wrapper);
        if(systemApiAuthority != null){
            throw new BusinessException(SystemRespCode.API_AUTHORITY_IS_EXIST);
        }
        BeanUtils.copyProperties(request,systemApiAuthority);
        systemApiAuthorityMapper.insert(systemApiAuthority);
    }

    @Override
    public void updateSystemApiAuthority(SystemApiAuthorityUpdateRequest request) {
        LambdaQueryWrapper<SystemApiAuthority> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SystemApiAuthority::getApiCode,request.getApiCode());
        SystemApiAuthority systemApiAuthority = systemApiAuthorityMapper.selectOne(wrapper);
        if(systemApiAuthority != null){
            throw new BusinessException(SystemRespCode.API_AUTHORITY_IS_EXIST);
        }
        LambdaUpdateWrapper<SystemApiAuthority> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(SystemApiAuthority::getId, request.getId());
        updateWrapper.set(SystemApiAuthority::getApiCode,request.getApiCode());
        updateWrapper.set(SystemApiAuthority::getApiName,request.getApiName());
        updateWrapper.set(SystemApiAuthority::getOrderNum,request.getOrderNum());
        updateWrapper.set(SystemApiAuthority::getPath,request.getPath());
        systemApiAuthorityMapper.update(null,updateWrapper);
    }

    @Override
    public void deleteSystemApiAuthority(Long systemApiAuthorityId) {
        SystemApiAuthority systemApiAuthority = systemApiAuthorityMapper.selectById(systemApiAuthorityId);
        systemApiAuthority.setIsDeleted(true);
        systemApiAuthorityMapper.updateById(systemApiAuthority);
        LambdaQueryWrapper<SystemRoleApiAuthority> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SystemRoleApiAuthority::getAuthorityId ,systemApiAuthorityId);
        systemRoleApiAuthorityMapper.delete(wrapper);
    }
}
